package cm.oneDimensional;

import java.util.Arrays;

public class NumberOfArithmeticSlicesDemo {
    /**
     * https://leetcode-cn.com/problems/arithmetic-slices/
     * 返回数组 A 中所有为等差数组的子数组个数
     * 状态转移方程：dp[i] = dp[i-1]+1
     */
    public int numberOfArithmeticSlices(int[] nums) {
        int[] dp = new int[nums.length];
        dp[0] = 0;
        for (int i = 2; i < nums.length; i++) {
            if (nums[i]-nums[i-1]==nums[i-1]-nums[i-2]){
                dp[i] = dp[i-1]+1;
            }
        }
        return Arrays.stream(dp).sum();
    }
}
